package com.training.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CarDAOJDBCImpl implements CarDAO {

	private String url;
	private String username;
	private String password;

	public CarDAOJDBCImpl(String url, String username, String password) {
		super();
		this.url = url;
		this.username = username;
		this.password = password;
	}

	@Override
	public List<Car> getAllCars() throws DAOException {

		List<Car> cars = new ArrayList<Car>();

		Connection con = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {

			con = createConnection();
			statement = con.createStatement();
			resultSet = statement.executeQuery("select * from car");

			while (resultSet.next()) {
				String vinNumber = resultSet.getString("vin_number");
				String make = resultSet.getString("make");
				String model = resultSet.getString("model");
				int year = resultSet.getInt("year");

				Car car = new Car(vinNumber, make, model, year);

				cars.add(car);

			}

		} catch (SQLException e) {
			throw new DAOException("Error while getting Cars", e);
		} catch (ClassNotFoundException ex) {
			throw new DAOException("Could not connect to DataBase", ex);
		} finally {

			closeResources(con, statement, resultSet);

		}

		return cars;

	}

	@Override
	public void addCar(Car car) throws DAOException {

		Connection con = null;
		PreparedStatement statement = null;

		try {
			con = createConnection();

			statement = con.prepareStatement("insert into car values(?,?,?,?)");

			statement.setString(1, car.getVinNumber());
			statement.setString(2, car.getMake());
			statement.setString(3, car.getModel());
			statement.setInt(4, car.getYear());

			statement.executeUpdate();

		} catch (ClassNotFoundException e) {
			throw new DAOException("Could not connect to DataBase", e);
		} catch (SQLException ex) {
			throw new DAOException("unable to execute operation", ex);
		} finally {

			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
	
	@Override
	public void deleteCar(int vin) {
		
		
		
	}

	private void closeResources(Connection con, Statement statement,
			ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private Connection createConnection() throws ClassNotFoundException,
			SQLException {
		Connection con;
		Class.forName("oracle.jdbc.driver.OracleDriver");
		con = DriverManager.getConnection(url, username, password);
		return con;
	}

	

}
